package com.java.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author
 * @date 2018/9/25 11:27
 */
@RestController
public class LoginController {
    @RequestMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        Map<String, String> user = (Map<String, String>) session.getAttribute("user");
        if (user!=null&&user.size()>=2){
            request.getRequestDispatcher("test.html").forward(request,response);
        }else {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
                //重新登录
                response.sendRedirect("/login.html");
            }else {
                Map<String,String> currentUser = new HashMap<String, String>();
                currentUser.put("username",username);
                currentUser.put("password",password);
                session.setAttribute("user",currentUser);
                response.sendRedirect("/login");
            }
        }

    }
}
